package com.ac.scum.log.task.face;


import com.ac.scum.log.task.cons.TaskConst;
import com.ac.scum.log.task.mapper.TaskLogItemCnNameKeyMapMapper;
import com.ac.scum.log.task.mapper.TaskLogItemCommandConvertCnNameKeyMapMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@RequiredArgsConstructor
public class InitTaskCache {
    private final StringRedisTemplate stringRedisTemplate;
    private final TaskLogItemCnNameKeyMapMapper taskLogItemCnNameKeyMapMapper;
    private final TaskLogItemCommandConvertCnNameKeyMapMapper taskLogItemCommandConvertCnNameKeyMapMapper;


    @PostConstruct
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void loadCache() {
        taskLogItemCnNameKeyMapMapper.selectList(Wrappers.emptyWrapper()).forEach(e -> stringRedisTemplate.opsForHash().put(TaskConst.LOG_CONVERT_KEY_MAP_CACHE, e.getKey().toLowerCase(), e.getConvert()));
        taskLogItemCommandConvertCnNameKeyMapMapper.selectList(Wrappers.emptyWrapper()).forEach(e -> stringRedisTemplate.opsForHash().put(TaskConst.LOG_CONVERT_KEY_MAP_CACHE, e.getCode().toLowerCase(), e.getName()));
        log.info("Convert Cache Load/ReLoad Success");
    }
}
